package com.Rsoft.goodWebsocketMan.handler;

import com.Rsoft.goodWebsocketMan.ui.listener.func.WebSocketListener;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;


public class MyClientHandler implements WebSocketHandler {
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("链接建立成功！");
        WebSocketListener.recvText("链接建立成功");
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        System.out.println("收到消息:" + message.getPayload());
        WebSocketListener.recvText("["+session.getRemoteAddress()+"] : " + message.getPayload());
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("链接出错！");
        WebSocketListener.recvText("链接出错");

    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
        System.out.println("链接已关闭！");
        WebSocketListener.recvText("链接已关闭");

    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}
